package com.hyd.admin.user.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.hyd.admin.resource.domain.Resource;
import com.hyd.admin.resource.service.IResourceService;
import com.hyd.admin.user.domain.User;
import com.hyd.admin.user.mapper.UserMapper;
import com.hyd.admin.user.service.IUserService;
import com.hyd.common.constant.ModuleConstant;

@Service
public class UserServiceImpl implements IUserService, UserDetailsService {

	private static Logger logger = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private IResourceService resourceService;

	@Override
	public User loadUserByUsername(String username)
			throws UsernameNotFoundException {
		User user = userMapper.loadUserByUsername(username);
		if (user == null) {
			logger.error(ModuleConstant.User.moduleName
					+ " exception when loadUserByUsername");
			throw new UsernameNotFoundException("username:" + username);
		}
		List<Resource> resourceList = resourceService
				.selectResourceListByUser(user.getUserId());
		user.setResourceList(resourceList);
		return user;
	}

}
